require "Net/send_Msg"
require 'View/UIBase/UIBase'

UITeamBonus = UIBase.New(PanelNames.UITeamBonus);
local pageType = 1;--0战斗加成页面 1 克制显示
local teamData = {}
function UITeamBonus.Init(data)
    pageType = data and 0 or 1;
    --[[
    data{
    table 当前阵容数据
    number 当前阵容索引
    table 当前替补
    }
    --]]
    teamData = data;
    UIBase.Init(UITeamBonus);
end

--定义控件
function UITeamBonus:OnCreate()
    --table
    --飞飞加成数据
    self.petShowCfg = {};
    --职业加成数量 controller列表
    self.occupationLevelsText = {};
    --职业加成数值 controller列表
    self.occupationDecsText = {};

    --list
    --飞飞加成列表
    self.petShowList = self:GetChild("listPetInfo");

    --controller
    --战斗加成控制器 0不显示 1显示
    self.addPageController = self:GetController("Info");
    --克制控制器 0不显示 1显示
    self.tipsPageController = self:GetController("TipsShow");
    --解锁控制 0所有 1仅职业 2职业+飞飞 3职业+祈福
    self.unLockController = self:GetController("InfoState");
    --text
    self.allHpText = self:GetChild("allHp");
    self.allAttackText = self:GetChild("allAttack");
    --祈福
    self.blessList = self:GetList("listNoticeBuff")
    local blessChild = self.blessList:GetChildAt(0)
    if blessChild then
        self.blessText = blessChild:GetChild("info")
        self.blessIcon = blessChild:GetChild("icon")

    end
    --btn
    self.backBtn = self:GetChild("bgclose");
    --初始化职业加成列表数据
    for i = 1, 6 do
        table.insert(self.occupationLevelsText, self:GetChild("occupationLevel" .. i):GetController("c1"));
        table.insert(self.occupationDecsText, self:GetChild("occupationDec" .. i):GetController("c1"));
    end
    self:addEvent();
end

function UITeamBonus:OnEnable()
    self.addPageController.selectedIndex = pageType == 1 and 0 or 1;
    self.tipsPageController.selectedIndex = pageType == 1 and 1 or 0;
    if pageType == 0 then
        self:initPetShow();
        self:refreshOccupationShow()

        self:RefreshBlessShow()
    end
end

function UITeamBonus:OnDisable()

end

function UITeamBonus:OnDestroy()

end

function UITeamBonus:addEvent()
    --btnChangeFormation.onClick:Add(function()
    --    self.ShowSelectFom(1);
    --end);
    self:RegisterOnClick(self.backBtn, self.onCloseClick);
    self:RegisterEvent(Config.EventType.TouchAnyKey, self.onCloseClick)

end

function UITeamBonus:removeEvent()
    self:UnRegisterEvent(Config.EventType.TouchAnyKey, self.onCloseClick);

end
function UITeamBonus:RefreshBlessShow()
    local cfg = G.dataTable["t_ecobuff"][tostring(logicMgr.NoticeManager.buffId)]--dataTable.getDataTableByStringId("t_skill",buffId)
    self.blessList.visible = logicMgr.NoticeManager.buffId > 0
    if cfg then
    local attID = decodeJsonStrToTabel(cfg.f_BuffEffectValue)[1]
        self.blessIcon.url = "ui://ICONAtlas/attr" .. attID;
        self.blessText.text = getLanguage(cfg.f_BuffDesc)
    else
        self.blessText.text = "    "
    end

end
--初始化
----初始化飞飞加成
function UITeamBonus:initPetShow()
    self.petShowCfg = {}
    if logicMgr.PetManager.EvoLevel ~= 0 then
        self.petShowCfg = decodeJsonStrToTabel(G.dataTable["t_elvesevolevel"][tostring(logicMgr.PetManager.EvoLevel)]["f_BattleTalentDesc"]);
        self.petIconCfg = decodeJsonStrToTabel(G.dataTable["t_elvesevolevel"][tostring(logicMgr.PetManager.EvoLevel)]["f_Attribute"]);
    end

    local isPetUnLock = logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Pet, false)--宠物是否解锁
    local isPray = FuncOpenManager.isOpen(Config.FuncOpenType.OperationNotice, false)
    if isPetUnLock and isPray and #self.petShowCfg > 0 then
        self.unLockController.selectedIndex = 0;
    elseif isPray and (not isPetUnLock or #self.petShowCfg == 0) then
        self.unLockController.selectedIndex = 3;
    elseif isPetUnLock and #self.petShowCfg > 0 and not isPray then
        self.unLockController.selectedIndex = 2;
    else
        self.unLockController.selectedIndex = 1;
    end

    self.petShowList.itemRenderer = self.rendererPetShow;
    self.petShowList.numItems = #self.petShowCfg;
    HelperFunc.ListTranstionPlay(self.petShowList)
end

--飞飞加成列表
function UITeamBonus.rendererPetShow(index, obj)
    local self = UITeamBonus;
    local idx = index + 1;
    obj:GetChild("info").text = getLanguage(self.petShowCfg[idx]);
    obj:GetChild("icon").url = "ui://ICONAtlas/attr" .. self.petIconCfg[idx][1]

end

--初始化职业加成
function UITeamBonus:refreshOccupationShow()
    local levelAdd = HeroManager.GetHeroAdd();--等级
    local cfg = G.dataTable["t_professionaddition"];
    local occupationIdx = {}
    for k, v in pairs(teamData.tabHeroPosData) do
        if occupationIdx[v["type"]] ~= nil then
            occupationIdx[v["type"]] = occupationIdx[v["type"]] + 1;
        else
            if v["type"] ~= nil then
                occupationIdx[v["type"]] = 1;
            end
        end
    end

    if teamData.helpMan[teamData.formationsIndex]["type"] ~= nil then
        if occupationIdx[teamData.helpMan[teamData.formationsIndex]["type"]] == nil then
            occupationIdx[teamData.helpMan[teamData.formationsIndex]["type"]] = 1;
        else
            occupationIdx[teamData.helpMan[teamData.formationsIndex]["type"]] = occupationIdx[teamData.helpMan[teamData.formationsIndex]["type"]] + 1;
        end
    end

    for i = 1, 6 do
        local addCfg = decodeJsonStrToTabel(cfg[tostring(i)]["f_AttributeAddition"]);
        local level = levelAdd[i];
        local addNum = 0;
        if level > 0 then
            addNum = addCfg[levelAdd[i]][2] / 100;
        end
        self.occupationLevelsText[i].selectedIndex = level;
        self.occupationDecsText[i].selectedIndex = level;


    end
    local hpCount = 0
    local attackCount = 0;
    for i, v in pairs(occupationIdx) do
        local addCfg = decodeJsonStrToTabel(cfg[tostring(i)]["f_AttributeAddition"]);
        if levelAdd[i] ~= 0 then
            local addId = addCfg[levelAdd[i]][1];
            local addCount = addCfg[levelAdd[i]][2] / 100;
            if EnumConst.FighterAttributeEnum.DAMAGE == addId then
                attackCount = attackCount + addCount * v;
            end
            if EnumConst.FighterAttributeEnum.HP_PER == addId then
                hpCount = hpCount + addCount * v;
            end
        end
    end

    self.allHpText.text = hpCount .. "%";
    self.allAttackText.text = attackCount .. "%";

end

-------------------------------按钮监听
---关闭页面
function UITeamBonus:onCloseClick()
    closeUI(PanelNames.UITeamBonus);
end

return UITeamBonus;